World

Column

Cumulative Confirmed Cases

Column

World level

Recent Cumulative Cases in the World
Date Confirmed Deaths Recovered
2020-04-08 1511104 88338 324507

Region level

Recent Cumulative Cases in Each Region

---
title: "Coronavirus Disease (COVID-19) Daily Report"
author: Jiaying Wu
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    theme: cerulean
---

```{r setup, include=FALSE}
library(tidyverse)
library(lubridate)
library(plotly)
library(ggthemes)
library(knitr)
library(DT)
```

World
=======================================================================

Column {data-width=620}
-----------------------------------------------------------------------

### Cumulative Confirmed Cases

```{r country_code}
# get country code
code <- read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv',
                 col_types = cols(
                   COUNTRY = col_character(),
                   `GDP (BILLIONS)` = col_double(),
                   CODE = col_character()
                 )) %>%
  select(COUNTRY, CODE) %>%
  rename(Region = COUNTRY,
         Code = CODE) %>%
  rownames_to_column("id")
code$id <- as.integer(code$id)
```

```{r case_cum_country}
# aggerate daily case summary in region level
case_cum <- read_csv("novel-corona-virus-2019-dataset/covid_19_data.csv",
                     col_types = cols(
                       SNo = col_double(),
                       ObservationDate = col_character(),
                       `Province/State` = col_character(),
                       `Country/Region` = col_character(),
                       `Last Update` = col_character(),
                       Confirmed = col_double(),
                       Deaths = col_double(),
                       Recovered = col_double()
                     )) %>%
  select(ObservationDate, `Country/Region`, Confirmed, Deaths, Recovered) %>%
  rename(Region = `Country/Region`,
         Date = ObservationDate) %>%
  mutate(Date = mdy(Date)) %>%
  group_by(Date, Region) %>%
  summarise(Confirmed = sum(Confirmed),
            Deaths = sum(Deaths),
            Recovered = sum(Recovered)) %>%
  ungroup()
```

```{r unique_region}
# extract unique region name
unique_region <- tibble("Region" = unique(case_cum$Region))
```

```{r replace_region_name}
# Rename the unmatched region name in code
code$Region <- code$Region %>%
  str_replace(pattern = "China", replacement = "Mainland China") %>%
  str_replace(pattern = "Korea, South", replacement = "South Korea") %>%
  str_replace(pattern = "United States", replacement = "US") %>%
  #str_replace(pattern = "", replacement = "Ivory Coast") %>%
  str_replace(pattern = "United Kingdom", replacement = "UK") %>%
  str_replace(pattern = "Macedonia", replacement = "North Macedonia")
  #str_replace(pattern = "", replacement = "Saint Barthelemy") %>%
```

```{r recent_case_cumulative}
# recent cumulative case and join daily case summary with code name
recent_case_cum <- case_cum %>%
  group_by(Region) %>%
  summarise(
    Date = last(Date),
    Confirmed = last(Confirmed),
    Deaths = last(Deaths),
    Recovered = last(Recovered)
  ) %>%
  left_join(code, by = "Region") %>%
  arrange(desc(Confirmed))
```

```{r set_map}
#Set country boundaries as light grey
line <- list(color = toRGB("#d1d1d1"), width = 0.2)

#Specify map projection and options
geo <- list(
     showframe = FALSE,
     showcoastlines = FALSE,
     projection = list(type = 'orthographic'),
     resolution = '100',
     showcountries = TRUE,
     countrycolor = '#d1d1d1',
     showocean = TRUE,
     oceancolor = '#064273',
     showlakes = TRUE,
     lakecolor = '#99c0db',
     showrivers = TRUE,
     rivercolor = '#99c0db',
     bgcolor = '#e8f7fc')

```

```{r confirm_map_3d}
confirm_map_3d <- plot_geo() %>%
  layout(geo = geo,
         paper_bgcolor = '#e8f7fc') %>%
  add_trace(data = recent_case_cum,
            z = ~Confirmed, 
            color = ~Confirmed, 
            colors = 'Reds',
            text = ~Region,
            locations = ~Code, 
            marker = list(line = line)) %>%
  colorbar(title = 'Confirmed cases')

confirm_map_3d
```

Column {data-width=380 .tabset}
-----------------------------------------------------------------------

### World level

##### Recent Cumulative Cases in the World

```{r time_series}
# time series of confirmed cases in each area
ts_confirmed <- read_csv("novel-corona-virus-2019-dataset/time_series_covid_19_confirmed.csv", 
                         col_types = cols(
                           .default = col_double(),
                           `Province/State` = col_character(),
                           `Country/Region` = col_character()
                         )) %>%
  select(-c("Lat", "Long")) %>%
  pivot_longer(cols = -c(`Province/State`, `Country/Region`), names_to = "Date", values_to = "Confirmed") %>%
  mutate(Date = mdy(Date))

# time series of recovered cases in each area
ts_recovered <- read_csv("novel-corona-virus-2019-dataset/time_series_covid_19_recovered.csv",
                         col_types = cols(
                           .default = col_double(),
                           `Province/State` = col_character(),
                           `Country/Region` = col_character()
                         )) %>%
  select(-c("Lat", "Long")) %>%
  pivot_longer(cols = -c(`Province/State`, `Country/Region`), names_to = "Date", values_to = "Recovered") %>%
  mutate(Date = mdy(Date))

# time series of deaths cases in each area
ts_deaths <- read_csv("novel-corona-virus-2019-dataset/time_series_covid_19_deaths.csv",
                      col_types = cols(
                        .default = col_double(),
                        `Province/State` = col_character(),
                        `Country/Region` = col_character()
                      )) %>%
  select(-c("Lat", "Long")) %>%
  pivot_longer(cols = -c(`Province/State`, `Country/Region`), names_to = "Date", values_to = "Deaths") %>%
  mutate(Date = mdy(Date))

# time series of 3 cases in each area
ts_all <- ts_confirmed %>%
  left_join(ts_recovered, by = c("Province/State", "Country/Region", "Date")) %>%
  mutate(Recovered = replace_na(Recovered, replace = 0)) %>%
  left_join(ts_deaths, by = c("Province/State", "Country/Region", "Date"))
```

```{r}
# aggregate in date level
ts_all_date <- ts_all %>%
  rename(Region = `Country/Region`) %>%
  group_by(Date) %>%
  summarise(Confirmed = sum(Confirmed),
            Deaths = sum(Deaths),
            Recovered = sum(Recovered))

kable(ts_all_date %>%
        filter(Date == max(Date)))
```

```{r barchart_cum}
ts_date_long <- ts_all_date %>% 
    select(-Confirmed) %>%
    pivot_longer(cols = -Date, names_to = "Status", values_to = "Cases")
barchart <- ggplot(data = ts_all_date, aes(x = Date)) +
  geom_bar(aes(y = Confirmed), position = "stack", stat = "identity", fill = "#ff5050") +
  geom_bar(data = ts_date_long, aes(y = Cases, fill = Status), position = "stack", stat = "identity") +
  scale_fill_manual(values = c("#000000", "#009900"))+
  theme_solarized(base_size = 10, light = TRUE)+
  theme(plot.margin = margin(0, 0, 0, 0, "pt"),
        panel.background = element_rect(fill = "White"),
        legend.position = "bottom",
        axis.title = element_blank(),
        #axis.text.x = element_blank(),
        axis.ticks = element_blank()) +
  ggtitle("COVID-19 Daily Cumulative Cases Bar Chart")

ggplotly(barchart) %>% 
  layout(legend = list(orientation = 'h'))
```


### Region level

Recent Cumulative Cases in Each Region

```{r table_case}
ts_all %>%
  rename(Region = `Country/Region`) %>%
  group_by(Region, Date) %>%
  summarise(Confirmed = sum(Confirmed),
            Deaths = sum(Deaths),
            Recovered = sum(Recovered)) %>%
  filter(Date == max(Date)) %>%
  arrange(desc(Confirmed)) %>%
  datatable(
    rownames = FALSE,
    fillContainer = TRUE,
    options = list(
      bPaginate = FALSE)
  )
```